<template>
    <el-button @click="handleClick" type="primary">
        <slot></slot>
    </el-button>
    <div class="m-choose-icon-dialog-body-height">
        <el-dialog :title="title" v-model="dialogVisible">
            <div class="container">
                <div @click="clickItem(item)" class="item" v-for="(item, index) in Object.keys(ElIcons)" :key="index">
                    <div>
                        <component :is="`el-icon-${toLine(item)}`"></component>
                    </div>
                    <div>{{ item }}</div>
                </div>
            </div>
        </el-dialog>
    </div>
</template>
<script lang="ts" setup>
import { toLine } from '../../../utils'
import * as ElIcons from '@element-plus/icons'
import { watch, ref } from 'vue'
import { useCopy } from '../../../hooks/useCopy'
let props = defineProps<{
    // 弹出框的标题
    title: string,
    // 控制弹出框的显示与隐藏
    visible: boolean
}>()

let emits = defineEmits(['update:visible'])
// 拷贝一份父组件传递过来的visible
let dialogVisible = ref<boolean>(props.visible)

// 点击按钮显示弹窗
let handleClick = () => {
    emits('update:visible', !props.visible)
}

watch(() => props.visible, val => {
    dialogVisible.value = val
})

watch(() => dialogVisible.value, val => {
    emits('update:visible', val)
})

// 点击图标
const clickItem = (item:string) => {
    let text = `<el-icon-${toLine(item)}>`
    // 复制组件
    useCopy(text)
    // 关闭弹窗
    dialogVisible.value = false
}
</script>
<style lang="scss" scoped>
.el-dialog__body {
    height: 500px;
    overflow: scroll;
}

.container {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
}

.container .item {
    width: 20%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin-bottom: 25px;
    cursor: pointer;
    font-size: 12px;
}

svg {
    width: 2em;
    height: 2em;
}
</style>